package org.blade.personal.framework.jdbc.stmt;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.blade.personal.framework.dsl.sql.Sql;
import org.blade.personal.framework.jdbc.StatementSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Simple statement setter
 * 
 * @author ZGF
 * @version 0.1
 */
public class SimpleStatementSetter implements StatementSetter {

	/**
	 * 日志记录
	 */
	private static final Logger logger = LoggerFactory.getLogger(SimpleStatementSetter.class);

	private Connection conn;

	/**
	 * SimpleStatementSetter 构造方法
	 * 
	 * @param conn
	 */
	public SimpleStatementSetter(Connection conn) {
		this.conn = conn;
	}

	@Override
	public void execute() {

	}

	@Override
	public ResultSet executeQuery(String sql) {

		logger.info("查询执行开始：");
		long start = System.currentTimeMillis();
		try {
			Connection conn = this.conn;
			Statement statement = conn.createStatement();
			statement.execute(sql);
			logger.info("execute Sql ->" + sql);
			ResultSet rs = statement.getResultSet();
			long cost = System.currentTimeMillis() - start;
			logger.info("查询执行耗时：" + cost + " Millis ");
			return rs;
		} catch (SQLException e) {
			logger.error(" 创建 Statement 实例出错!" + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ResultSet executeQuery(Sql sql) {
		
		return this.executeQuery(sql.get());
	}
}
